import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:lazy_food/models/image_model.dart';
import 'package:lazy_food/util/string+.dart';

class ImageView extends StatelessWidget {
  List<ImageModel> images;
  double width;
  double height;
  ImageView(
      {Key? key,
      required this.images,
      required this.width,
      required this.height})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (images.length <= 0) {
      return Container();
    } else if (images.length == 1) {
      return ClipRRect(
        borderRadius: BorderRadius.circular(10),
        child: CachedNetworkImage(
          imageUrl: images.first.url_pattern.toImage(),
          width: width,
          height: height,
          fit: BoxFit.cover,
        ),
      );
    } else if (images.length == 2) {
      return ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(10)),
        child: SizedBox(
          width: width,
          height: height,
          child: Row(
            children: [
              CachedNetworkImage(
                imageUrl: images.first.url_pattern.toImage(),
                width: width / 2.0,
                height: height,
                fit: BoxFit.cover,
              ),
              CachedNetworkImage(
                imageUrl: images.last.url_pattern.toImage(),
                width: width / 2.0,
                height: height,
                fit: BoxFit.cover,
              ),
            ],
          ),
        ),
      );
    } else if (images.length == 3) {
      return ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(10)),
        child: SizedBox(
          width: width,
          height: height,
          child: Row(
            children: [
              CachedNetworkImage(
                imageUrl: images.first.url_pattern.toImage(),
                width: width / 2.0,
                height: height,
                fit: BoxFit.cover,
              ),
              Column(
                children: [
                  CachedNetworkImage(
                    imageUrl: images[1].url_pattern.toImage(),
                    width: width / 2.0,
                    height: height / 2.0,
                    fit: BoxFit.cover,
                  ),
                  CachedNetworkImage(
                    imageUrl: images[2].url_pattern.toImage(),
                    width: width / 2.0,
                    height: height / 2.0,
                    fit: BoxFit.cover,
                  ),
                ],
              ),
            ],
          ),
        ),
      );
    } else {
      return ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(10)),
        child: SizedBox(
          width: width,
          height: height,
          child: Row(
            children: [
              Column(
                children: [
                  CachedNetworkImage(
                    imageUrl: images[0].url_pattern.toImage(),
                    width: width / 2.0,
                    height: height / 2.0,
                    fit: BoxFit.cover,
                  ),
                  CachedNetworkImage(
                    imageUrl: images[1].url_pattern.toImage(),
                    width: width / 2.0,
                    height: height / 2.0,
                    fit: BoxFit.cover,
                  ),
                ],
              ),
              Column(
                children: [
                  CachedNetworkImage(
                    imageUrl: images[2].url_pattern.toImage(),
                    width: width / 2.0,
                    height: height / 2.0,
                    fit: BoxFit.cover,
                  ),
                  CachedNetworkImage(
                    imageUrl: images[3].url_pattern.toImage(),
                    width: width / 2.0,
                    height: height / 2.0,
                    fit: BoxFit.cover,
                  ),
                ],
              ),
            ],
          ),
        ),
      );
    }
  }
}
